package ynu.edu.merchant.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import ynu.edu.common.result.Result;
import ynu.edu.merchant.dto.MerchantDTO;
import ynu.edu.merchant.dto.MerchantLoginDTO;
import ynu.edu.merchant.service.MerchantService;
import ynu.edu.merchant.vo.MerchantVO;

import java.util.List;

/**
 * 商家控制器
 */
@RestController
@RequestMapping("/merchants")
@RequiredArgsConstructor
public class MerchantController {

    private final MerchantService merchantService;

    /**
     * 创建商家
     */
    @PostMapping("/register")
    public Result<MerchantVO> createMerchant(@Validated(MerchantDTO.Create.class) @RequestBody MerchantDTO merchantDTO) {
        MerchantVO merchantVO = merchantService.createMerchant(merchantDTO);
        return Result.success(merchantVO);
    }
    
    /**
     * 商家登录
     */
    @PostMapping("/login")
    public Result<String> login(@RequestBody @Valid MerchantLoginDTO loginDTO) {
        String token = merchantService.login(loginDTO);
        return Result.success(token);
    }
    
    /**
     * 获取当前登录商家信息
     */
    @GetMapping("/current")
    public Result<MerchantVO> getCurrentMerchantInfo(HttpServletRequest request) {
        // 从请求属性中获取商家ID
        Long merchantId = (Long) request.getAttribute("merchantId");
        if (merchantId == null) {
            return Result.unauthorized();
        }
        
        // 获取商家信息
        MerchantVO merchantVO = merchantService.getMerchantById(merchantId);
        return Result.success(merchantVO);
    }

    /**
     * 更新商家信息
     */
    @PutMapping("/{id}")
    public Result<MerchantVO> updateMerchant(@PathVariable Long id, @RequestBody @Valid MerchantDTO merchantDTO) {
        merchantDTO.setId(id);
        MerchantVO merchantVO = merchantService.updateMerchant(merchantDTO);
        return Result.success(merchantVO);
    }

    /**
     * 获取商家信息
     */
    @GetMapping("/{id}")
    public Result<MerchantVO> getMerchantById(@PathVariable Long id) {
        MerchantVO merchantVO = merchantService.getMerchantById(id);
        return Result.success(merchantVO);
    }

    /**
     * 删除商家
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteMerchant(@PathVariable Long id) {
        boolean result = merchantService.deleteMerchant(id);
        return Result.success(result);
    }

    /**
     * 根据分类ID查询商家列表
     */
    @GetMapping("/category/{categoryId}")
    public Result<Page<MerchantVO>> getMerchantsByCategoryId(
            @PathVariable Long categoryId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<MerchantVO> merchantPage = merchantService.getMerchantsByCategoryId(categoryId, page, size);
        return Result.success(merchantPage);
    }

    /**
     * 根据商家名称模糊查询
     */
    @GetMapping("/search")
    public Result<Page<MerchantVO>> searchMerchantsByName(
            @RequestParam String name,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        Page<MerchantVO> merchantPage = merchantService.searchMerchantsByName(name, page, size);
        return Result.success(merchantPage);
    }

    /**
     * 获取推荐商家列表
     */
    @GetMapping("/recommended")
    public Result<List<MerchantVO>> getRecommendedMerchants() {
        List<MerchantVO> merchants = merchantService.getRecommendedMerchants();
        return Result.success(merchants);
    }

    /**
     * 更新商家状态
     */
    @PutMapping("/{id}/status")
    public Result<Boolean> updateMerchantStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean result = merchantService.updateMerchantStatus(id, status);
        return Result.success(result);
    }
} 